/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.core.framework;

import com.openwaf.common.annotation.EnsureIdentifier;


public class BlockMethod {
    /*
     * Count of number of 
     */
    private static int B=0;
    private static int R=1;
    private static int N=2;
    private static int c=0;
    

    private static Object g;
    // ov_getGCCount
    public BlockMethod(){
        this.init();
    }
    @EnsureIdentifier
    public final native void init()/*-{
       this.g={};
    }-*/;
    @EnsureIdentifier
    public int n(){
        return c++;
    }
    //getState
    @EnsureIdentifier
    public native int s(int g)/*-{
        if(this.g[g]==undefined){ return -1; }
        return this.g[g].s;
    }-*/;
    //setState
    @EnsureIdentifier
    public native void t(int g,int s)/*-{
        this.g[g].s=s;
    }-*/;
    //initBFCall
    /*
     * r .. return values
     * cb   call back
     * co   calling object
     * ac   argument count
     * ex   exception
     * x    xmlhttp object
     * s    current state
     * m    method stack
     */
    @EnsureIdentifier
    public native void i(int g)/*-{
        this.g[g]={};
        this.g[g].r=null;
        this.g[g].cb=null;
        this.g[g].co=null;
        this.g[g].ac=null;
        this.g[g].ex=null;
        this.g[g].x=null;
        this.g[g].m=[];
        this.g[g].m[0]={};
        this.g[g].m[0].v=[]
        this.g[g].m[0].r=-1;
        this.g[g].s=this.N;
    }-*/;
    //initBFCallFun
    @EnsureIdentifier
    public native void f(int g,int m)/*-{
        this.g[g].m[m]={};
        this.g[g].m[m].v=[]
        this.g[g].m[m].r=-1;
        this.g[g].s=this.N;
    }-*/;
    //setRC
    @EnsureIdentifier
    public native void l(int g,int m,int c)/*-{
        this.g[g].m[m].r=c;
    }-*/;
    //getRC
    @EnsureIdentifier
    public native int k(int g,int m)/*-{
        return this.g[g].m[m].r;
    }-*/;
    //setVar
    @EnsureIdentifier
    public native void m(int g,int m,String n,Object v)/*-{
        this.g[g].m[m].v[n]=v;
    }-*/;
    //getVar
    @EnsureIdentifier
    public native int p(int g,int m,String n)/*-{
        return this.g[g].m[m].v[n];
    }-*/;
    //getXMLHTTPObject
    @EnsureIdentifier
    public native Object o()/*-{
		if (window.XMLHttpRequest){
			return new XMLHttpRequest();
		}
		if (window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
		return null;
    }-*/;
    //getXMLDoc
    @EnsureIdentifier
    public native Object x(String s)/*-{
		if (window.DOMParser){			
			return (new DOMParser()).parseFromString(s,"text/xml");
		}
		x=new ActiveXObject("Microsoft.XMLDOM");
		x.async="false";
		x.loadXML(s);
        return x;
    }-*/;
    @EnsureIdentifier
   	private native Object xn(Object o) /*- {
		var n,i,r=[];
		for(i=0;i<o.childNodes.length;i++){
			n=o.childNodes[i];
			if(n.nodeType==1){r.push(n);}
		}
		return r;
	} -*/;
    @EnsureIdentifier
    public native String s_s(String[] o)/*- {
	    try{
		var i=0,s="";
		for(i=0;i<o.length;i++){
			if(i>0) s+=",";
			s+=("\"" + encodeURI(o[i]) + "\"");
		}
		return "[" + s + "]";
	    }
	    catch(e){}
	    return "";

	} -*/;
    @EnsureIdentifier
	public native String s_n(int[] o)/*- {
	    try{
		return "[" + o.join(",") +"]";
	    }
	    catch(e){}
	    return "";

	} -*/;
}
